/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.area.model;

import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D.Double;
import javax.swing.JComponent;
import org.opu.db_vdumper.ui.component.area.TmplDrawer;
import org.opu.db_vdumper.ui.component.tabel.views.ArrowView;
import org.opu.db_vdumper.ui.component.tabel.views.TableView;

/**
 *  Drag it`s whn "some time" heapen mouse event - mouse presed,
 * then pointer mose moved some where, and not already released.
 * In this reason component start moving, but not already moved,
 * so needed to draw temple presentation of moveble component.
 * @author Potochnak Yana
 */
public class DraggePresentor {
    private MouseEvent mousePressedEvent;
    private Component comp;
    private Container main;
    protected Point old;
    protected Point point;
    protected Dimension sz;

    /**
     *  Invoke pressed mouse action
     * @param e
     */
    public void pressed(MouseEvent e) {
        mousePressedEvent = e;
        if (isDragged()){
            Container c = e.getComponent().getParent();
            c.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
    }

    /**
     *  Invoke released mouse action
     * @param e
     */
    public void released(MouseEvent e) {
        if (main != null){
            main.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } else if (e != null){
            Container c = e.getComponent().getParent();
            c.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }

        if (e != null){
            if (comp instanceof TableView){
                TableView tv = (TableView) comp;

                tv.setLocation(point);
                tv.getViewModel().revalidateArrows();
                
                if (main instanceof JComponent) {
                    ((JComponent) main).revalidate();
                } else {
                    main.repaint();
                }
            }
        }
        
        stop();
    }

    /**
     *  Invoke dragged action
     * @param e mouseDragged event
     * @param p mousePreased event thet heapen later
     */
    public void dragg(MouseEvent e){
        if (isDragged()) {
            MouseEvent p = mousePressedEvent;
            comp = e.getComponent();
            if (comp instanceof ArrowView){
                draggArrow((ArrowView) comp);
                return;
            }

            main = comp.getParent();
            Dimension s = comp.getPreferredSize();
            Point pt = comp.getLocation();

            old = point;
            point = new Point(pt.x + e.getX() - p.getX(), pt.y + e.getY() - p.getY());
            sz = new Dimension(s.width - 2, s.height - 2);
            paint();
        }
    }

    /**
     *  Invoke if dragged action is ArrowView
     * @param arrow arrow
     */
    private void draggArrow(ArrowView arrow){

    }

    /**
     *  Is draggeded some object now
     * @return true is was mouse pressed event
     */
    public boolean isDragged(){
        return mousePressedEvent != null;
    }

    /** Stop dragged */
    public void stop(){
        if (main != null) {
            if (main instanceof TmplDrawer) {
                ((TmplDrawer) main).deleteTmp();
            }

            cleareSquare(old, main, sz);
            cleareSquare(point, main, sz);
        }

        mousePressedEvent = null;
        comp = null;
        main = null;
        old = null;
        point = null;
        sz = null;
    }

    /**  paint temple component on root pane */
    private void paint(){
        if (main != null) {
            if (main instanceof TmplDrawer) {
                Double r = new Double(point.x, point.y, sz.width, sz.height, 10, 10);
                ((TmplDrawer) main).setTmp(r);
            }

            cleareSquare(old, main, sz);
        }
    }

    private void cleareSquare(Point p, Container c, Dimension sz){
        if (c != null){
            if (p != null){
                int v = 3;
                c.repaint(p.x - v, p.y - v, sz.width + v + v, sz.height + v + v);
            }
        }
    }
}
